<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <canvas id="canvas-a" class="canvas" width="700" height="100"></canvas>
    <canvas id="canvas-b" class="canvas" width="700" height="100"></canvas>
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="terrainExaggeration" :max="2" /></div> -->
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="relativeHeight" :max="10000" /></div> -->
      <!-- <div class="select-box-wrapper"><select-box :options="layerOptions" /></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="shadows">shadows</a-checkbox></div> -->
      <!-- <div class="select-box-wrapper"><select-box :options="entityToolbarOptions" /></div>
        <div class="select-box-wrapper"><select-box :options="sizeOptions" /></div>
        <div class="select-box-wrapper"><a-checkbox v-model:checked="softShadows">soft shadows</a-checkbox></div>
        <div class="select-box-wrapper"><a-checkbox v-model:checked="terrainShadows">terrain shadows</a-checkbox></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="picking">选择</a-checkbox></div> -->
      <div id="menu">
        <!-- <p><button @click="onClick('play')">播放</button></p> -->
        <!-- <p><button @click="onClick('stop')">暂停</button></p> -->
      </div>
    </div>

    <a-dropdown v-if="false">
      <a class="ant-dropdown-link tools" @click.prevent>
        {{ drawingModeText }}
        <DownOutlined />
      </a>
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item v-for="(e, index) in menuOptions" :key="e.key">{{ e.text }}</a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
  </div>
</template>

<script>
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';
import SelectBox from '@components/widgets/select-box';
import {g3a_odlines} from '@lib/g3x';
import { G3V } from '@lib/g3x';

export default defineComponent({
  name: 'buslines', // 航线
  components: { DownOutlined, EditOutlined, SelectBox },
  setup() {
    return {
      viewer: null,
      onshadows: () => {},
      isRepeating: true,
      videoOverlay: true
    };
  },
  data() {
    return {
      _odlines: undefined,
      layerOptions: [],
      shadows: true,
      nowPostion: {}
    };
  },
  computed: {},
  watch: {
    shadows(val) {
      this.onshadows(val);
    }
  },
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    handleMenuClick(e) {
      this.drawingMode = e.key;
    },
    handleClick(value) {
      const viewer = this.viewer;
      switch (value) {
        case 'isRepeating':
          break;
      }
    },
    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },
    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.default;

      const czmOptions = {
        imageryProvider: { options: { "url": '//zzgis.com/cdn/3d/Assets/Textures/NaturalEarthII', "fileExtension": 'jpg', brightness: 0.1, show: true } },
        shadows: true
      };
      const g3vOptions = {
        navigation: true,
        depthTestAgainstTerrain: false,
        hideLayers: false,
        // addImageLayers: [{ type: 'tdtIboMap', options: { brightness: 0.5, show: true } }, { type: 'tdtImgAnno', options: { brightness: 0.5, show: true } }]
      };

      var g3v = new G3V();
      var viewer = g3v.create('cesium-drawer', czmOptions, g3vOptions);

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },
    initEffects() {
      let vm = this;
      const viewer = this.viewer;
      var scene = viewer.scene;

      function loadData(dataFunc) {
        Cesium.Resource.fetchJson('//zzgis.com/cdn/3d/SampleData/earthSDK/odlines/lines-bus.json').then(function(data) {
          //var timeDuration = 5.0;
          //var moveBaseDuration = 0.5;
          var timeDuration = 10.0;
          var moveBaseDuration = 4.0;

          var hStep = 300 / (data.length - 1);

          // var busLines = [].concat.apply([], data.map(function (busLine, idx) {
          var busLines = [];
          data.map(function(busLine, idx) {
            var prevPt;
            var points = [];
            for (var i = 0; i < busLine.length; i += 2) {
              var pt = [busLine[i], busLine[i + 1]];
              if (i > 0) {
                pt = [prevPt[0] + pt[0], prevPt[1] + pt[1]];
              }
              prevPt = pt;

              // points.push([pt[0] / 1e4, pt[1] / 1e4]);
              var longitude = ((pt[0] / 1e4) * Math.PI) / 180.0;
              var latitude = ((pt[1] / 1e4) * Math.PI) / 180.0;
              // var cart = Cesium.Cartesian3.fromDegrees(longitude, latitude, 100.0);
              points.push([longitude, latitude]);
            }

            busLines.push({
              positions: points,
              color: [Math.random() * 0.5 + 0.5, Math.random() * 0.8 + 0.2, 0.0, 1.0],
              width: 2.0,
              startTime: timeDuration * Math.random(),
              duration: moveBaseDuration + 1.0 * Math.random()
            });
          });

          // var busLinesCount = busLines.length;
          // var busLinesPointsCount = 0;
          // busLines.forEach(function (busLine) {
          //     busLinesPointsCount += busLine.positions.length;
          // });
          // console.log('busLinesCount: ' + busLinesCount);
          // console.log('busLinesPointsCount: ' + busLinesPointsCount);

          // var routePaths = busLines;
          dataFunc(busLines, timeDuration);
        });
      }

      this._earth = viewer;

      this._odlines = new g3a_odlines(viewer);
      this._odlines.translucentPass = false;
      this._odlines.color = [1, 1, 1, 1];

      loadData((data, timeDuration) => {
        this._odlines.timeDuration = timeDuration;
        this._odlines.data = data;
        this._odlines.playing = true;
      });

      // 1.1.3 数据绑定
      // 双向绑定的属性，如果一方发生变化，另一方同样会跟随变化
      // this._fpsUnbind = XE.MVVM.bind(this, 'fps', earth.status, 'fps');
      // this._elapsedTimeUnbind = XE.MVVM.bind(this, 'elapsedTime', earth.status, 'elapsedTime');

      // only for Debug
      window.earth = viewer;
      window.odlines = this._odlines;

      const position = [2.033082406731791, 0.6932973408430964, 25375.718759865656];
      const rotation = [5.907289830535223, -0.907166753023871, 0.00031189810712373145];

      // viewer.camera.position = position;
      // viewer.camera.rotation = rotation;

      viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromRadians(2.033082406731791, 0.6932973408430964, 25375.718759865656),
        orientation: {
          heading: Cesium.Math.toRadians(175.0),
          pitch: Cesium.Math.toRadians(-35.0),
          roll: 0.0
        },
        orientation: {
          heading: 5.907289830535223,
          pitch: -0.907166753023871,
          roll: 0.00031189810712373145
        }
      });
    }
  }
});
</script>

<style lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .cesium-viewer-bottom {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }

  .tools {
    position: absolute;
    top: 57px;
    left: 7px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.6;
    transition: all 500ms;
    &:hover {
      opacity: 0.8;
    }
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}
#trailer {
  position: absolute;
  bottom: 75px;
  right: 0;
  width: 320px;
  height: 180px;
}
.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 18em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }

  .select-box-wrapper {
    padding: 5px 15px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
    width: auto;
  }
}
</style>
